home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / util1 / shell-10.rea < prev    next >
Text File  |  1996-01-16  |  6KB  |  180 lines

  1. Short:    New shell commands (IF, FOR) +source
  2. Author:   Aaron "Optimizer" Digulla (digulla@fh-konstanz.de)
  3. Uploader: Aaron "Optimizer" Digulla (digulla@fh-konstanz.de)
  4. Type:     util/sys
  5.  
  6. This directory contains a couple of commands which replace existing
  7. CLI commands or introduce new ones. The commands have been written
  8. with the goal of maximum compatibility in mind. All commands accept
  9. Amiga and UNIX-bourne-shell compatible argument lists. Full source
  10. is included. If you unpack the archive, specify "-a" as option to
  11. preserve the pure bits or set them later. All commands are pure and
  12. can be made resident (some even have to be).
  13.  
  14. FOR
  15. ---
  16.  
  17.     FOR var IN [list] [;] DO [;] [cmdlist] [;] DONE
  18.  
  19. Note that you must enclose ; in "" as in:
  20.  
  21.     FOR test IN a b c ";" DO echo $test ";" DONE
  22.  
  23. All ";" are optional except inbetween commands in the cmdlist:
  24.  
  25.     FOR dir IN c devs l DO cd SYS:$dir ";" dir ";" cd / DONE
  26.  
  27. Also note that a CD changes the current dir and if you want to
  28. execute a series of commands in a sub-dir, you must go up again
  29. OR put the commands in a "sub shell":
  30.  
  31.     FOR dir IN c devs l DO ( cd SYS:$dir ";" dir ) DONE
  32.  
  33. does the same as the previous version. Also note that the variables
  34. you use are not unset after the loop. thus:
  35.  
  36.     FOR test IN a b c ";" DO echo $test ";" DONE
  37.     FOR test IN a b c ";" DO echo $test ";" DONE
  38.  
  39. yields: "a b c c c c". This is due to the fact that $test is unknown
  40. when the first loop is evaluated, but when the shell reads the next
  41. line, $test is equivalent to "c" and therefore, FOR is called with
  42. "... DO echo c ; DONE" as arguments. To prevent this, you must unset
  43. the variable after (or before use):
  44.  
  45.     unset test
  46.     FOR test IN a b c ";" DO echo $test ";" DONE
  47.     unset test
  48.     FOR test IN a b c ";" DO echo $test ";" DONE
  49.  
  50. It is safe to use unset on a non-existing variable.
  51.  
  52. IF
  53. --
  54.  
  55. IF accepts the normal arguments as the standard Amiga IF and these:
  56.  
  57.     IF [ testlist ] [; THEN]
  58.     IF cmd [; THEN]
  59.  
  60. Note that testlist has to be enclosed in [] (and they must be separated
  61. by at least a single space from all other arguments) while cmd just
  62. follows IF. If you want to put the THEN in the same line as the if,
  63. you must put ";" before it (WITH the "" !!!).
  64.  
  65. Since the cmd-type is more simple, I describe it first: The command
  66. "cmd" is executed and if no error occurred, this is taken as success
  67. (ie. the condition holds) and therefore the IF body is executed.
  68. If the command fails, the ELSE part (if there is one) is executed
  69. instead.
  70.  
  71. The testlist-type is a short cut for the cmd-type. You can think of
  72.  
  73.     IF [ testlist ]
  74.  
  75. beeing a simple form of
  76.  
  77.     IF TEST testlist
  78.  
  79. TEST is a program which reads it's arguments, interprets them and
  80. returns SUCCESS or ERROR if the arguments are "true". Example:
  81.  
  82.     IF [ aaa = bbb ]
  83.  
  84. is the same as
  85.  
  86.     IF TEST aaa = bbb
  87.  
  88. and is false, since "aaa" is _not_ "bbb". The = is case sensitive, while
  89. the Amiga version (IF aaa EQ bbb) is not:
  90.  
  91.     IF aaa EQ AAA    -> TRUE
  92.     IF [ aaa = AAA ]    -> FALSE
  93.  
  94. Test has been built into IF to speed things up a little. It takes the
  95. following options:
  96.  
  97. Examine files:
  98.  
  99.   -G FILE     FILE exists and is owned by the effective group ID
  100.   -L FILE     FILE exists and is a symbolic link
  101.   -O FILE     FILE exists and is owned by the effective user ID
  102.   -S FILE     FILE exists and is a socket
  103.   -b FILE     FILE exists and is block special
  104.   -c FILE     FILE exists and is character special
  105.   -d FILE     FILE exists and is a directory
  106.   -e FILE     FILE exists
  107.   -f FILE     FILE exists and is a regular file
  108.   -g FILE     FILE exists and is set-group-ID
  109.   -k FILE     FILE exists and has its sticky bit set
  110.   -p FILE     FILE exists and is a named pipe
  111.   -r FILE     FILE exists and is readable
  112.   -s FILE     FILE exists and has a size greater than zero
  113.   -t          standard output is opened on a terminal
  114.   -t FD       file descriptor FD is opened on a terminal
  115.   -u FILE     FILE exists and its set-user-ID bit is set
  116.   -w FILE     FILE exists and is writable
  117.   -x FILE     FILE exists and is executable
  118.  
  119.   FILE1 -ef FILE2   FILE1 and FILE2 have the same device and inode numbers
  120.   FILE1 -nt FILE2   FILE1 is newer (modification date) than FILE2
  121.   FILE1 -ot FILE2   FILE1 is older than FILE2
  122.  
  123. Examine strings:
  124.  
  125.   STRING           the length of STRING is non-zero (same as below)
  126.   -n STRING           the length of STRING is non-zero
  127.   -z STRING           the length of STRING is zero
  128.   STRING1 = STRING2    the strings are equal
  129.   STRING1 != STRING2   the strings are not equal
  130.  
  131. Examine numbers:
  132.  
  133.   INTEGER1 -eq INTEGER2   INTEGER1 is equal to INTEGER2
  134.   INTEGER1 -ge INTEGER2   INTEGER1 is greater than or equal to INTEGER2
  135.   INTEGER1 -gt INTEGER2   INTEGER1 is greater than INTEGER2
  136.   INTEGER1 -le INTEGER2   INTEGER1 is less than or equal to INTEGER2
  137.   INTEGER1 -lt INTEGER2   INTEGER1 is less than INTEGER2
  138.   INTEGER1 -ne INTEGER2   INTEGER1 is not equal to INTEGER2
  139.  
  140. Misc:
  141.  
  142.   ( EXPRESSION )               EXPRESSION is true
  143.   ! EXPRESSION               EXPRESSION is false
  144.   EXPRESSION1 -a EXPRESSION2   both EXPRESSION1 and EXPRESSION2 are true
  145.   EXPRESSION1 -o EXPRESSION2   either EXPRESSION1 or EXPRESSION2 is true
  146.  
  147. There are several special files that are part of IF:
  148.  
  149.     ELSE        This replaces the Amiga ELSE command
  150.     ELIF, ELSEIF    These are mere copies of IF (you can use links if
  151.             you want) to keep the shell happy.
  152.     FI            This is a synonym for ENDIF
  153.     THEN        A nop
  154.  
  155. With these extra files, you can use real sh syntax:
  156.  
  157.     IF [ -f xxxx ]
  158.     THEN
  159.     echo xxxx exists
  160.     ELSE
  161.     echo xxxx doesn't exist
  162.     FI
  163.  
  164. To install the new version, it's not sufficient to copy the files somewhere
  165. in your path, you must also overwrite the resident version (you can
  166. undo that easily if you run into problems). All commands can be made
  167. resident but the absolute minimum is this:
  168.  
  169.     Resident IF Add
  170.     Resident ELSE Add
  171.  
  172. You can remove them later with
  173.  
  174.     Resident IF Remove
  175.     Resident ELSE Remove
  176.  
  177. to get the standard commands back again. It's a good idea to make
  178. all the other commands (ELSEIF, ELIF, THEN, etc.) resident, too,
  179. to gain speed.
  180.